import django_filters
from apps.HttpTestcase.models import Testcases


class TestcaseFilters(django_filters.rest_framework.FilterSet):
    # iexact表示精确匹配, 并且忽略大小写
    # icontains表示模糊查询（包含），并且忽略大小写
    # exact表示精确匹配
    project = django_filters.CharFilter(method='project_filter', lookup_expr='exact')
    testsuite = django_filters.CharFilter(method='testsuite_filter', lookup_expr='exact')
    module = django_filters.CharFilter(method='module_filter')
    api = django_filters.CharFilter(field_name='api')
    api_name = django_filters.CharFilter(method='api_name_filter')
    name = django_filters.CharFilter(field_name='name', lookup_expr='icontains')
    status = django_filters.CharFilter(field_name='status', lookup_expr='exact')
    level = django_filters.CharFilter(field_name='level', lookup_expr='exact')

    def project_filter(self, queryset, name, value):
        return queryset.filter(api__project_id=value)

    def testsuite_filter(self, queryset, name, value):
        # return queryset.filter(testsuite__id=value).order_by('testsuite2testcase')
        return queryset.filter(testsuite__id=value)

    def module_filter(self,queryset,name,value):
        return queryset.filter(api__module_id=value)

    def api_name_filter(self,queryset, name, value):
        return queryset.filter(api__name=value)

    class Meta:
        model = Testcases
        fields = ['project', 'testsuite', 'api', 'module']
